/**
 * @(#)EmailSendServiceImpl.java 2017年02月13日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.service.impl;

import java.io.IOException;
import java.util.List;

import org.jsbd.boss.dao.IEmailGroupDao;
import org.jsbd.boss.dao.IEmailInfoDAO;
import org.jsbd.boss.domian.EmailGroupBean;
import org.jsbd.boss.domian.EmailInfo;
import org.jsbd.boss.service.IEmailSendService;
import org.jsbd.boss.util.EmailSslUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 
 * 邮件发送服务类
 * 
 * @author <a href="mailto:zhouchao@xingbook.com" >周超</a>
 * @version 1.0.0
 */
@Service("emaillService")
public class EmailSendServiceImpl implements IEmailSendService {

	private static final Logger LOGGER = LoggerFactory.getLogger(EmailSendServiceImpl.class);

	@Autowired
	private EmailSslUtils emailUtil;
	@Autowired
	private IEmailInfoDAO emailInfoDAO;
	@Autowired
	private IEmailGroupDao emailGroupDAO;

	@Override
	public void sendEmail(String[] toPeople, String subject, String content, String[] attchmentFileList)
			throws IOException {
		emailUtil.setTo(toPeople);
		emailUtil.setSubject(subject);
		emailUtil.setContent(content);
		emailUtil.setAttchmentFileList(attchmentFileList);
		emailUtil.send();
	}

	@Override
	public void sendEmailToSysAccount(String subject, String content, String[] attchmentFileList) throws IOException {
		List<EmailInfo> emailInfoList = emailInfoDAO.getAllEmailInfo();
		if (emailInfoList == null || emailInfoList.isEmpty()) {
			LOGGER.error("数据库中获取不到收件人信息");
			throw new IOException("数据库中获取不到收件人信息");
		}

		int len = emailInfoList.size();
		String[] toPeople = new String[len];
		for (int i = 0; i < len; i++) {
			toPeople[i] = emailInfoList.get(i).getEmail();
		}

		emailUtil.setTo(toPeople);
//		emailUtil.setCc(toPeople);
		emailUtil.setSubject(subject);
		emailUtil.setContent(content);
		emailUtil.setAttchmentFileList(attchmentFileList);
		emailUtil.send();
	}
	
	@Override
	public void sendEmailToSysAccountGroup(String subject, String content, String[] attchmentFileList, String group) throws IOException {
		EmailGroupBean emailGroup = new EmailGroupBean();
		if (("Y2").equals(group)) {
			emailGroup.setRnum("Y2");
			emailGroup = emailGroupDAO.getEmailGroup(emailGroup);
		}
		if (("Y1").equals(group)) {
			emailGroup.setRnum("Y1");
			emailGroup = emailGroupDAO.getEmailGroup(emailGroup);
		}
		List<EmailInfo> emailInfoList = emailInfoDAO.getAllEmailGroupInfo(emailGroup);
		if (emailInfoList == null || emailInfoList.isEmpty()) {
			LOGGER.error("数据库中获取不到收件人信息");
			throw new IOException("数据库中获取不到收件人信息");
		}

		int len = emailInfoList.size();
		String[] toPeople = new String[len];
		for (int i = 0; i < len; i++) {
			toPeople[i] = emailInfoList.get(i).getEmail();
		}

		emailUtil.setTo(toPeople);
//		emailUtil.setCc(toPeople);
		emailUtil.setSubject(subject);
		emailUtil.setContent(content);
		emailUtil.setAttchmentFileList(attchmentFileList);
		LOGGER.info("EMAIL TO SEND...");
		emailUtil.send();
	}
	

}
